package org.dubnation.car.controller;

import org.dubnation.car.domain.CarInfoVO;
import org.dubnation.car.service.ICarService;
import org.dubnation.car.domain.Car;
import org.dubnation.car.query.CarQuery;
import org.dubnation.base.util.AjaxResult;
import org.dubnation.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    public ICarService carService;


    /**
     * 保存和修改公用的
     * @param car  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult insertOrUpdate(@RequestBody Car car, HttpServletRequest request, HttpServletResponse response){
        if( car.getId()!=null)
            carService.update(car);
        else{
            String token = request.getHeader("token");
            carService.insertCar(car, token);

        }
        return AjaxResult.me();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        carService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        Car car = carService.queryById(id);
        return AjaxResult.me().setData(car);
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult queryAll(){
        List< Car> list = carService.queryAll();
        return AjaxResult.me().setData(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody CarQuery query){
        PageInfo<Car> pageInfo = carService.queryPage(query);
        return AjaxResult.me().setData(pageInfo).setMessage("分页查询成功");
    }

    /**
    * 批量删除数据
    * @param ids
    * @return
    */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        carService.batchDelete(ids);
        return AjaxResult.me();
    }

    /**
     * 上架车辆
     * @param ids
     * @return
     */
    @PostMapping("/onSale")
    public AjaxResult onSale(@RequestBody List<Long> ids, HttpServletRequest request){
        AjaxResult onSaleResult = carService.onSale(ids, request);
        if (Objects.nonNull(onSaleResult)) return onSaleResult;
        else return AjaxResult.me();
    }

    /**
     * 下架车辆
     * @param ids
     * @return
     */
    @PatchMapping("/offSale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        carService.offSale(ids);
        return AjaxResult.me();
    }

    /**
     * 获取车辆信息 (车辆详情、店铺信息、售卖人员信息、车辆资源)
     * @param id
     * @return
     */
    @GetMapping("/carInfo/{id}")
    public AjaxResult getCarInfo(@PathVariable("id") Long id){
        CarInfoVO carData = carService.getCarInfo(id);
        return AjaxResult.me().setData(carData);
    }
}
